const Router = require('koa-router')
const router = new Router()
const { httpRequest } = require('../../utils/request')
const { getDayTime } = require('../../utils/util')

/**
 * 热门列表
 * @method GET
 * @param day 星期数[0-6]不传默认为今天的热门数据
 * @param gender 性别,0:女、1:男
 * 接口调用: /api/mhList/hot?day=8&gender=0
*/
router.get('/hot',async (ctx,next)=>{
    const query = ctx.request.query
    let url = `https://api.kkmh.com/v1/daily/comic_lists/${getDayTime(query.day || 0)/1000}`
    const res = await httpRequest({
        url,
        data:{
            new_device:false,
            since:0,
            gender:query.gender
        },
        method:'get'
    })
    ctx.body = res.data
})
/**
 * 榜单
 * @method GET
 * @param type 排行类型：2：新作榜、3：完结榜、4：畅销榜、5：男生榜、6：女生榜、 7：20+最爱榜
 * @param since 偏移量（分页）
 * @param limit 列表数（分页）
 * 接口调用: /api/mhList/toppic?type=2&since=0&limit=30
*/
router.get('/toppic',async (ctx,next)=>{
    const { type, since, limit } = ctx.request.query
    let url = `http://www.kuaikanmanhua.com/v2/topic/rank_list/${type||2}`
    const res = await httpRequest({
        url,
        data:{
            since:since||0,
            limit:limit||30
        },
        method:'get'
    })
    ctx.body = res.data
})

/**
 * 分类列表
 * @method GET
 * 接口调用: /api/mhList/taglist
*/
router.get('/taglist',async (ctx,next)=>{
    const { type, since, limit } = ctx.request.query
    let url = `https://api.kkmh.com/v1/topic_new/lists/get_by_tag`
    const res = await httpRequest({
        url,
        data:{
            count:30,
            since:0,
            tag:1
        },
        method:'get'
    })
    ctx.body = res.data
})

/**
 * 热门列表
 * @method GET
 * @param since 偏移量（分页）
 * @param limit 列表数（分页）
 * @param classid 分类id
 * @param gender 性别
 * @param sort 排序方式
 * @param pay_status 付费模式
 * @param update_status 更新状态
 * 接口调用: /api/mhList/classify
*/
router.get('/classify',async (ctx,next)=>{
    const { type, since, limit, classid, gender, sort, pay_status, update_status } = ctx.request.query
    let url = `https://api.kkmh.com/v1/topic_new/lists/get_by_tag`
    const res = await httpRequest({
        url,
        data:{
            count:limit || 20,
            since:since || 0,
            tag:classid || 49,
            gender:gender || 1,
            sort:sort || 1,
            query_category:`{pay_status : ${pay_status || -1},update_status : ${update_status || -1}}`
        },
        method:'get'
    })
    ctx.body = res.data
})
/**
 * 轮播图
 * @method GET
 * 接口调用: /api/mhList/banner
*/
router.get('/banner',async (ctx,next)=>{
    let url = `https://ios-globalapi.321mh.com/app_api/v5/getbookbytype/?platformname=iphone&page=1&pagesize=20`
    const res = await httpRequest({
        url,
        data:{
            gender:1,
            operator_count:9
        },
        method:'get'
    })
    for(let i = 0; i<res.data.data.book.length; i++){
        let item = res.data.data.book[i]
        for (let j = 0; j < item.comic_info.length; j++) {
            item.comic_info[j].img_url = 'http://image.samanlehua.com/'+item.comic_info[j].img_url
            
        }
    }
    ctx.body = res.data.data.book[1].comic_info
})


/**
 * 搜索提示
 * @method GET
 * 接口调用: /api/mhList/hot_search
*/
router.get('/prompt',async (ctx,next)=>{
    const {keyword} = ctx.request.query
    let url = `https://search.kkmh.com/v1/search/suggestion_topic_author`
    const res = await httpRequest({
        url,
        data:{
            f:2,
            type:0,
            q:keyword || '',
        },
        method:'get'
    })
    ctx.body = res.data
})

/**
 * 搜索结果
 * @method GET
 * 接口调用: /api/mhList/search_result
*/
router.get('/search_result',async (ctx,next)=>{
    const {keyword} = ctx.request.query
    let url = `https://search.kkmh.com/search/multi`
    const res = await httpRequest({
        url,
        data:{
            entrance:1,
            q:keyword
        },
        method:'get'
    })
    ctx.body = res.data
})

/**
 * 获取首页内容
 * @method post
 * 接口调用: /api/mhList/get_homedata
*/
router.get('/get_homedata',async (ctx,next)=>{
    let url = `https://api.kkmh.com/v1/topic_new/discovery_list`
    const res = await httpRequest({
        url,
        data:{gender:null},
        method:'get'
    })
    ctx.body = res.data
})
/**
 * 获取首页内容
 * @method get
 * 接口调用: /api/mhList/feedList
*/
router.get('/feedList',async (ctx,next)=>{
    let url = `https://social.kkmh.com/v1/graph/feedList?feedListType=4&limit=20&since=0`
    const res = await httpRequest({
        url,
        data:{gender:null},
        method:'get'
    })
    ctx.body = res.data
})


module.exports = router
